// mockjs 默认只能用于测试环境，依赖也是安装在测试上，正式打包之后无法使用 mockjs
import Mock from "mockjs";

// 设置拦截ajax请求的相应时间
Mock.setup({ timeout: "0" });

// 自动注册 mock 服务
//
const defaultData = { status: 200, success: true, msg: "操作成功"};
// 1 获取数据
const datas = require.context("./data", true, /\.js$/);

// 2 解析数据  并注册 mock 服务
datas.keys().forEach((key) => {
  const d = datas(key).default;
  if (!d) return;
  //
  d.forEach((item) => {
    const disabled = item.disabled;
    if (disabled) {
      return;
    }
    const url = item.url;
    const method = item.method || "";
    const mock = item.mock || {};
    const tm = typeof mock;
    const res = item.result || {};

    Mock.mock(url, method, (option) => {
      const result =
        tm === "function" ? mock.call(this, option, Mock) : Mock.mock(mock);
      return { ...defaultData,...res, result };
    });
  });
});
